// components/cascader/cascader.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 弹出弹层标识
    flag: {
      type: Boolean,
      value: false,
      observer(newVal, oldVal) {
        this.setData({
          show: true,
          selectedArr: [],
        })
      },
    },
    groupId: {
      type: String,
      value: '',
      observer(newVal, oldVal) {
        if(newVal) this.getList(newVal)
      },
    },
    // 列表选项数据
    list: {
      type: Array,
      value: [],
      observer(newVal, oldVal) {
        if(newVal && newVal.length) {
          this.setData({
            optList: newVal,
          })
        }
      },
    },
    // 选项 id别称
    oid: {
      type: String,
      value: 'id',
    },
    // 选项 name别称
    name: {
      type: String,
      value: 'classify_name',
    },
    // 级联已选
    selected: {
      type: Array,
      value: [],
      observer(newVal, oldVal) {
        this.setData({
          selectedArr: newVal,
        })
      },
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    optList: [],

    selectedItem: {},

    childFlag: false,

    selectedArr: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    select(e) {
      let { childFlag, selectedArr, name, oid, } = this.data,
          item = e.detail,
          obj = {
            selectedItem: item,
          }
      selectedArr.push(item)
      obj.selectedArr = selectedArr
      if(item.children) {
        obj.childFlag = !childFlag
      } else {
        this.triggerEvent('select', {
          val: selectedArr,
          name,
          id: oid,
        })
      }
      this.setData(obj)
    },
    close(e) {
      let { selectedArr, name, oid, } = this.data
      this.triggerEvent('select', {
        val: selectedArr,
        name,
        id: oid,
      })
    },
    selectCascader(e) {
      this.triggerEvent('select', e.detail)
    },

    // 接口
    getList(groupid) {
      wx.$post(3, 'Classify/query_all_classify', { group_id: groupid, }, true, true).then(res => {
        this.setData({
          optList: res,
        })
      })
    },
  }
})
